//
//  ChooseProjectViewController.m
//  IpadR
//
//  Created by edianzu on 2017/6/3.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "ChooseProjectViewController.h"
#import "ChooseProjectViewCell.h"

@interface ChooseProjectViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray * proArr;//测试组数组
@property (nonatomic,strong) NSMutableArray * inageIndex;//8个项目数组
@property (nonatomic,copy) NSIndexPath * index;

@end

@implementation ChooseProjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择项目";
    self.view.backgroundColor = [HexColor colorWithHexString:Gray_Back];
    //创建返回按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(0, 0, 25,25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"NavBack"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBarBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];;
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = 15;
    //将两个BarButtonItem都返回给NavigationItem
    self.navigationItem.leftBarButtonItems = @[spaceItem,leftBarBtn];
    //如果没有本地数据 从新创建
    NSUserDefaults * userd = [NSUserDefaults standardUserDefaults];
//    [userd removeObjectForKey:@"testProject"];
//    [userd removeObjectForKey:@"untestProject"];
    NSMutableArray * arr = [NSMutableArray new];
    NSMutableArray * arr1 = [NSMutableArray new];
    arr = [userd objectForKey:@"testProject"];
    arr1 = [userd objectForKey:@"untestProject"];
    
    NSLog(@"%ld----------%ld",arr.count,arr1.count);
    if (arr.count ==0) {
        self.proArr = [[NSMutableArray alloc] init];
        NSMutableArray * proDetailArr = [[NSMutableArray alloc] init];
        [self.proArr addObject:proDetailArr];
    }else{
        self.proArr = [[NSMutableArray alloc] init];
//        self.proArr = [[NSMutableArray alloc] initWithArray:arr];
        
        for (NSArray * dataarr in arr) {
            NSMutableArray * proDetailArr = [[NSMutableArray alloc] init];
            for (NSMutableDictionary * dic in dataarr) {
                
                [proDetailArr addObject:dic];
                
            }
         [self.proArr addObject:proDetailArr];
        }
    }
    if (arr1.count ==0 && arr.count ==0) {
        NSDictionary * dic1 = @{@"proNum":@"1",@"proName":@"身高"};
        NSDictionary * dic2 = @{@"proNum":@"2",@"proName":@"体重"};
        NSDictionary * dic3 = @{@"proNum":@"3",@"proName":@"10米折返跑"};
        NSDictionary * dic4 = @{@"proNum":@"4",@"proName":@"坐位体前屈"};
        NSDictionary * dic5 = @{@"proNum":@"5",@"proName":@"立定跳远"};
        NSDictionary * dic6 = @{@"proNum":@"6",@"proName":@"双脚连续跳"};
        NSDictionary * dic7 = @{@"proNum":@"7",@"proName":@"网球掷远"};
        NSDictionary * dic8 = @{@"proNum":@"8",@"proName":@"走平衡木"};
        NSMutableArray * aray = [[NSMutableArray alloc]init];
        [aray addObject:dic1];
        [aray addObject:dic2];
        [aray addObject:dic3];
        [aray addObject:dic4];
        [aray addObject:dic5];
        [aray addObject:dic6];
        [aray addObject:dic7];
        [aray addObject:dic8];
        self.inageIndex = [[NSMutableArray alloc] initWithArray:aray];
    }else{
        self.inageIndex = [[NSMutableArray alloc] initWithArray:arr1];
    }
    [self createUI];
}
//系统返回按钮方法
-(void)leftBarBtnClicked{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createUI{
    //下方添加测试组
    self.addLabel = [[UILabel alloc]init];
    self.addLabel.font = [UIFont systemFontOfSize:16*AutoScaleHorizontal];
    self.addLabel.text = @"添加测试组";
    self.addLabel.textColor = [UIColor blackColor];
    [self.addLabel setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:self.addLabel];
    [self.addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-40*AutoScaleVertical);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-50*AutoScaleHorizontal);
        make.size.mas_equalTo(CGSizeMake(100*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.addBtn setImage:[UIImage imageNamed:@"addGroup"] forState:UIControlStateNormal];
    [self.view addSubview:self.addBtn];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.addLabel.mas_centerX);
        make.bottom.mas_equalTo(self.addLabel.mas_top).mas_offset(-20*AutoScaleVertical);
        make.width.mas_offset(60*AutoScaleHorizontal);
        make.height.mas_offset(60*AutoScaleVertical);
    }];
    [self.addBtn addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    
    //  列表
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = [HexColor colorWithHexString:Gray_Back];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.bottom.mas_equalTo(self.view.mas_bottom);
//        make.width.mas_equalTo(600*AutoScaleVertical);
//        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.left.right.mas_equalTo(self.view);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 500;
    self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedSectionHeaderHeight = 300;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[ChooseProjectViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view bringSubviewToFront:self.addBtn];
    [self.view bringSubviewToFront:self.addLabel];
}

#pragma mark tableViewDelegate

//禁止TABLEVIEW 下拉顶部超过SCREEN
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    NSLog(@"%f",self.childTable.contentOffset.y);
    if (self.tableView.contentOffset.y <= 0) {
        self.tableView.bounces = NO;
        NSLog(@"禁止下拉");
    }
    else
        if (self.tableView.contentOffset.y >= 0){
            self.tableView.bounces = YES;
            NSLog(@"允许上拉");
        }

}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView  * view = [[UIView alloc]init];
    view.backgroundColor = [HexColor colorWithHexString:Gray_Back];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20*AutoScaleVertical;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    return  [self getCellHeight:self.proArr[indexPath.section]] * 180 * AutoScaleVertical;
//    
//    
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.proArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChooseProjectViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[ChooseProjectViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell configUIWithArr:self.proArr[indexPath.section] andNumber:indexPath.section];
    NSLog(@"pro数组长度为%ld",self.proArr.count);
    __weak typeof(self) weak_self = self;
    //添加项目
    cell.callAction = ^{
//        [weak_self createIconUI];
        weak_self.index = indexPath;
        [self addTrueView:_inageIndex andnum:indexPath.section];
    };
    //删除组
    cell.callDelete = ^{
        weak_self.index = indexPath;
        [self sureToDelete];
    };
    //删除小项目
    cell.callDeleteSmall = ^(NSInteger tag) {
        weak_self.index = indexPath;
        NSMutableArray * arr = self.proArr[indexPath.section];
        NSMutableDictionary * dic = arr[tag];
        [self.inageIndex addObject:dic];
        //对剩余项目数组从新排序
        for (int  i =0; i<[self.inageIndex count]-1; i++) {
            
            for (int j = i+1; j<[self.inageIndex count]; j++) {
                
                if ([[self.inageIndex[i] objectForKey:@"proNum"] intValue]>[[self.inageIndex[j] objectForKey:@"proNum"] intValue]) {
                    
                    //交换
                    
                    [self.inageIndex exchangeObjectAtIndex:i withObjectAtIndex:j];
                    
                }
                
            }
            
        }
        
        
        
        [arr removeObjectAtIndex:tag];
        self.proArr[indexPath.section] = arr;
        
        [self.tableView reloadData];
    };
    
    return cell;
}



#pragma mark  添加测试组
- (void)addAction{
    if ( self.proArr.count < 8) {//最多8组
        NSMutableArray * proDetailArr = [[NSMutableArray alloc] init];
        [self.proArr addObject:proDetailArr];
        [self.tableView reloadData];
    }
    else{
        NSLog(@"已经有8组了");
    }
    
    
}

#pragma mark 选择添加项目弹出视图
- (void)addTrueView:(NSMutableArray*)dataArray andnum:(NSInteger)section{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.52;
    [window addSubview:_backView];
    
    _backWhiteView = [[UIView alloc]init];
    _backWhiteView.backgroundColor = [UIColor whiteColor];
    [window addSubview:_backWhiteView];
    [_backWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backView.mas_centerX);
        make.top.mas_equalTo(_backView.mas_top).with.offset(160*AutoScaleVertical);
        make.width.mas_equalTo(600*AutoScaleHorizontal);
        if (dataArray.count<5) {
            make.height.mas_equalTo(300*AutoScaleVertical);
        }else{
            make.height.mas_equalTo(400*AutoScaleVertical);
        }
    }];
    
    self.backLabel = [[UILabel alloc]init];
    self.backLabel.font = [UIFont systemFontOfSize:16*AutoScaleHorizontal];
    self.backLabel.textColor = [UIColor blackColor];
    [self.backLabel setTextAlignment:NSTextAlignmentLeft];
    [window addSubview:self.backLabel];
    [self.backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backWhiteView.mas_left).with.offset(30*AutoScaleHorizontal);
        make.top.mas_equalTo(self.backWhiteView.mas_top).with.offset(20*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(200*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.backDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.backDeleteBtn.backgroundColor = UIColorFromRGB(49, 183, 188);
    [self.backDeleteBtn setImage:[UIImage imageNamed:@"iconCancel"] forState:UIControlStateNormal];
    [self.backDeleteBtn addTarget:self action:@selector(pressDelete) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:self.backDeleteBtn];
    [self.backDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backWhiteView.mas_right).with.offset(-30*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.backLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.backLineView = [[UIView alloc]init];
    self.backLineView.backgroundColor =UIColorFromRGB(241, 241, 241);
    [window addSubview:self.backLineView];
    [self.backLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backWhiteView.mas_top).with.offset(50*AutoScaleVertical);
        make.left.mas_equalTo(self.backWhiteView.mas_left);
        make.width.mas_equalTo(self.backWhiteView);
        make.height.mas_equalTo(1*AutoScaleVertical);
    }];
    
    //创建还剩下的项目
    int a = 0;
    
    for (NSMutableDictionary * dic in dataArray) {
        //带文字和图片的按钮
        UIButton * probtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        probtn.frame = CGRectMake((50+120*(a%4))*AutoScaleHorizontal, (130+130*(a/4)*AutoScaleVertical), 60*AutoScaleHorizontal, 80*AutoScaleVertical);
        probtn.selected = NO;
        probtn.tag = a + 1000;
        [probtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"project-%@",[dic objectForKey:@"proNum"]]] forState:UIControlStateNormal];
        [probtn setTitle:[dic objectForKey:@"proName"] forState:UIControlStateNormal];
        [probtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        probtn.titleLabel.font = [UIFont systemFontOfSize:12*AutoScaleHorizontal];
        //设置按钮图片在上文字在下
        probtn.imageEdgeInsets = UIEdgeInsetsMake(-30*AutoScaleVertical, 0, 0, 0);
        probtn.titleEdgeInsets = UIEdgeInsetsMake(50*AutoScaleVertical, -60*AutoScaleHorizontal, 0, 0);
        [probtn addTarget:self action:@selector(pressPro:) forControlEvents:UIControlEventTouchUpInside];
        [self.backWhiteView addSubview:probtn];
        [probtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.backWhiteView.mas_left).with.offset((60+130*(a%4))*AutoScaleHorizontal);
            make.top.mas_equalTo(self.backWhiteView.mas_top).with.offset((90+130*(a/4)*AutoScaleVertical));
            make.size.mas_equalTo(CGSizeMake(65*AutoScaleHorizontal, 90*AutoScaleVertical));
        }];
        
        //IMAGEVIEW 小对勾 标记按钮状态
        UIImageView * selectIMG = [[UIImageView alloc]init];
        selectIMG.image= [UIImage imageNamed:@"choosedBtn"];
        selectIMG.hidden = YES;
        selectIMG.tag = a + 2000;
        [probtn addSubview: selectIMG];
        [selectIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(probtn.mas_top).with.offset(-5*AutoScaleVertical);
            make.right.mas_equalTo(probtn.mas_right).with.offset(-5*AutoScaleHorizontal);
            make.size.mas_equalTo(CGSizeMake(25*AutoScaleHorizontal, 25*AutoScaleVertical));
        }];
        
        a++;
        if (a == dataArray.count) {
            break;
        }
    }
    
    //确认按钮
    self.backConfirmBtn = [[UIButton alloc]init];
    self.backConfirmBtn.layer.cornerRadius =10;
    [self.backConfirmBtn setTitle:@"确认" forState:UIControlStateNormal];
    self.backConfirmBtn.backgroundColor = UIColorFromRGB(87, 190, 184);
    [self.backConfirmBtn addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:self.backConfirmBtn];
    [self.backConfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.backWhiteView.mas_bottom).with.offset(-20*AutoScaleVertical);
        make.centerX.mas_equalTo(self.backWhiteView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(100*AutoScaleHorizontal  , 30*AutoScaleVertical));
    }];
    
    switch (section+1) {
        case 1:
            self.backLabel.text = @"添加第一组测试项目";
            break;
        case 2:
            self.backLabel.text = @"添加第二组测试项目";
            break;
        case 3:
            self.backLabel.text = @"添加第三组测试项目";
            break;
        case 4:
            self.backLabel.text = @"添加第四组测试项目";
            break;
        case 5:
            self.backLabel.text = @"添加第五组测试项目";
            break;
        case 6:
            self.backLabel.text = @"添加第六组测试项目";
            break;
        case 7:
            self.backLabel.text = @"添加第七组测试项目";
            break;
        case 8:
            self.backLabel.text = @"添加第八组测试项目";
            break;
        default:
            break;
    }
}

//选择8项目以用于添加
-(void)pressPro:(UIButton*)sender{
    
    sender.selected = !sender.selected;
    UIImageView * img = (UIImageView*)[sender viewWithTag:sender.tag+1000];
    NSLog(@"%ld,%ld",sender.tag,img.tag);
    NSDictionary * dic = self.inageIndex[sender.tag-1000];
    if (sender.selected ==YES) {
        img.hidden = NO;
        [self.proArr[self.index.section] addObject:dic];
    }else{
        img.hidden = YES;
        [self.proArr[self.index.section] removeObject:dic];
    }
}

//取消添加
-(void)pressDelete{
    [self.backView removeFromSuperview];
    [self.backWhiteView removeFromSuperview];
    [self.backDeleteBtn removeFromSuperview];
    [self.backConfirmBtn removeFromSuperview];
    [self.backLabel removeFromSuperview];
    [self.backLineView removeFromSuperview];
}

//确定选中
- (void)tapAction{

//    for (UIButton * btn in self.backWhiteView.subviews) {
//        if (btn.selected) {
//            [self.proArr[self.index.section] addObject:self.inageIndex[btn.tag-1000]];
//        }
//    }
    // 第二次循环删除数据源被选中的部分 由于写在上面的循环的话会造成 两个线程同时操作同一数组崩溃。
    NSMutableArray * selectArr = [NSMutableArray new];
    for (UIButton * btn in self.backWhiteView.subviews) {
        if (btn.selected) {
            [selectArr addObject:[NSString stringWithFormat:@"%ld",btn.tag - 1000]];
            //[self deleArr:btn.tag-1000+1];
        }
    }
    [self deleArr:selectArr];
    
    for (NSMutableDictionary * dic in self.inageIndex) {
        NSLog(@"剩下的项目%@",[dic objectForKey:@"proName"]);
    }
    

    [self.backView removeFromSuperview];
    [self.backWhiteView removeFromSuperview];
    [self.backDeleteBtn removeFromSuperview];
    [self.backConfirmBtn removeFromSuperview];
    [self.backLabel removeFromSuperview];
    [self.backLineView removeFromSuperview];
    [self.tableView reloadData];
}


//从总数组里减去选中的。
- (void)deleArr:(NSMutableArray*)arr{
    for (int i = (int)arr.count - 1; i>=0; i--) {
        int index = [arr[i] intValue];
        [self.inageIndex removeObject :self.inageIndex[index]];
    }
}

#pragma mark 删除组
-(void)sureToDelete{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.52;
    [window addSubview:_backView];
    
    _backWhiteView = [[UIView alloc]init];
    _backWhiteView.backgroundColor = [UIColor whiteColor];
    [window addSubview:_backWhiteView];
    [_backWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backView.mas_centerX);
        make.top.mas_equalTo(_backView.mas_top).with.offset(160*AutoScaleVertical);
        make.width.mas_equalTo(600*AutoScaleHorizontal);
        make.height.mas_equalTo(400*AutoScaleVertical);
    }];
    
    self.backLabel = [[UILabel alloc]init];
    self.backLabel.font = [UIFont systemFontOfSize:16*AutoScaleHorizontal];
    self.backLabel.text = @"提示";
    self.backLabel.textColor = [UIColor blackColor];
    [self.backLabel setTextAlignment:NSTextAlignmentCenter];
    [window addSubview:self.backLabel];
    [self.backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.backWhiteView.mas_centerX);
        make.top.mas_equalTo(self.backWhiteView.mas_top).with.offset(20*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(200*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];

    
    
    self.backLineView = [[UIView alloc]init];
    self.backLineView.backgroundColor =UIColorFromRGB(241, 241, 241);
    [window addSubview:self.backLineView];
    [self.backLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backWhiteView.mas_top).with.offset(50*AutoScaleVertical);
        make.left.mas_equalTo(self.backWhiteView.mas_left);
        make.width.mas_equalTo(self.backWhiteView);
        make.height.mas_equalTo(1*AutoScaleVertical);
    }];
    
    self.backLabel1 = [[UILabel alloc]init];
    self.backLabel1.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
    [self.backLabel1 setTextAlignment:NSTextAlignmentCenter];
    self.backLabel1.text = @"确定删除当前测试组吗？";
    self.backLabel1.textColor = [UIColor blackColor];
    [window addSubview:self.backLabel1];
    [self.backLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.backWhiteView.mas_centerX);
        make.top.mas_equalTo(self.backLineView.mas_top).with.offset(100*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];

    
    
    self.backDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backDeleteBtn setBackgroundImage:[UIImage imageNamed:@"noBtn"] forState:UIControlStateNormal];
    [self.backDeleteBtn addTarget:self action:@selector(sureNo) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:self.backDeleteBtn];
    [self.backDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backWhiteView.mas_left).with.offset(150*AutoScaleHorizontal);
        make.bottom.mas_equalTo(self.backWhiteView.mas_bottom).with.offset(-100*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(100*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];
    
    self.backConfirmBtn = [[UIButton alloc]init];
//    self.backConfirmBtn.layer.cornerRadius =10;
    [self.backConfirmBtn setBackgroundImage:[UIImage imageNamed:@"yesBtn"] forState:UIControlStateNormal];
    [self.backConfirmBtn addTarget:self action:@selector(sureYes) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:self.backConfirmBtn];
    [self.backConfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backWhiteView.mas_right).with.offset(-150*AutoScaleHorizontal);
        make.bottom.mas_equalTo(self.backWhiteView.mas_bottom).with.offset(-100*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(100*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];
}
//取消
-(void)sureNo{
    [self.backView removeFromSuperview];
    [self.backWhiteView removeFromSuperview];
    [self.backDeleteBtn removeFromSuperview];
    [self.backConfirmBtn removeFromSuperview];
    [self.backLabel removeFromSuperview];
    [self.backLineView removeFromSuperview];
    [self.backLabel1 removeFromSuperview];
}
//确定删除
-(void)sureYes{
    [self.backView removeFromSuperview];
    [self.backWhiteView removeFromSuperview];
    [self.backDeleteBtn removeFromSuperview];
    [self.backConfirmBtn removeFromSuperview];
    [self.backLabel removeFromSuperview];
    [self.backLineView removeFromSuperview];
    [self.backLabel1 removeFromSuperview];
    //取出删除组中的项目加到总数据源里
    NSMutableArray * addarr = [NSMutableArray new];
    addarr = self.proArr[self.index.section];
    [self.proArr removeObjectAtIndex:self.index.section];
    
    for (NSMutableDictionary * dic in addarr) {
        [self.inageIndex addObject:dic];
    }
    //对剩余项目数组从新排序
    for (int  i =0; i<[self.inageIndex count]-1; i++) {
        
        for (int j = i+1; j<[self.inageIndex count]; j++) {
            
            if ([[self.inageIndex[i] objectForKey:@"proNum"] intValue]>[[self.inageIndex[j] objectForKey:@"proNum"] intValue]) {
                
                //交换
                
                [self.inageIndex exchangeObjectAtIndex:i withObjectAtIndex:j];
                
            }
            
        }
        
    }
    
    [self.tableView reloadData];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    NSUserDefaults * userd = [NSUserDefaults standardUserDefaults];
    NSMutableArray * newProArr = [NSMutableArray new];
    for (NSMutableArray*arr in self.proArr) {
        if (arr.count!=0) {
            [newProArr addObject:arr];
        }
    }
    
    
    [userd setObject:newProArr forKey:@"testProject"];
    [userd setObject:self.inageIndex forKey:@"untestProject"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
